package com.chaodows.commons.dto.page;

import com.chaodows.commons.util.ObjectUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Objects;

@Data
@XmlRootElement(name = "hash")
@ApiModel("分页参数")
public class PaginationCondition implements Serializable {

    public static final int DEFAULT_PAGE = 1;
    private static final int DEFAULT_PAGE_SIZE = 15;
    private static final long serialVersionUID = -7710240600295999589L;
    @ApiModelProperty(value = "当前页面")
    private Integer page = DEFAULT_PAGE;
    @ApiModelProperty(value = "页面大小")
    private Integer pageSize = DEFAULT_PAGE_SIZE;
    private int offSet;

    public PaginationCondition() {
    }

    public PaginationCondition(Integer page, Integer pageSize) {
        if (page != null) {
            this.page = page;
        }
        if (pageSize != null) {
            this.pageSize = pageSize;
        }
    }

    public Integer getPage() {
        return Objects.isNull(page) ? DEFAULT_PAGE : page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPageSize() {
        return ObjectUtil.getInteger(pageSize, DEFAULT_PAGE_SIZE);
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public int getOffset() {
        return (getPage() - 1) * getPageSize();
    }

    @Override
    public String toString() {
        return "PaginationCondition [page=" + getPage() + ", pageSize=" + getPageSize() + "]";
    }
}
